<?php
namespace app\backend\task;

use app\backend\models\ArticleModel;
use lib\task\TaskJob;

/**
 * 文章发布Job
 * 所有文章改动 均以任务的方式存在
 * 需要后期开发补纯纯静态生成
 * Class PushArticle
 * @package app\backend\task
 */
class PushArticle implements TaskJob{

    public static $tree;

    public function objName()
    {
        return str_replace('\\',',',__CLASS__);
    }

    public function param()
    {
        return json_encode(self::$tree['param']?self::$tree['param']:[]);
    }

    public function frequency()
    {
        return @self::$tree['frequency']?self::$tree['frequency']:1;
    }

    public function delay()
    {
        return @self::$tree['delay']?self::$tree['delay']:10;
    }

    public function execute($param = [])
    {
        if (!empty($param)){
            self::$tree['param'] = $param;
        }
        $data = ['message' => '任务失败'];
        if (!empty(self::$tree['param'])) {
            if (empty(self::$tree['param']['article_id'])){
                $code = ArticleModel::find()->insert([
                    'article_title'    => self::$tree['param']['article_title'],
                    'article_describe' => self::$tree['param']['article_describe'],
                    'article_content'  => self::$tree['param']['article_content'],
                    'article_image'    => self::$tree['param']['article_image'],
                    'classify_id'      => self::$tree['param']['classify_id'],
                    'sort'             => self::$tree['param']['sort'],
                    'is_recommend'     => self::$tree['param']['is_recommend'],
                    'article_status'   => self::$tree['param']['article_status'],
                    'add_time'         => time()
                ]);
                if ($code > 0) {
                    $data = ['message' => '添加成功'];
                }
            }else{
                $code = ArticleModel::find()->where([['=', 'article_id', self::$tree['param']['article_id']]])->update([
                    'article_title'    => self::$tree['param']['article_title'],
                    'article_describe' => self::$tree['param']['article_describe'],
                    'article_content'  => self::$tree['param']['article_content'],
                    'article_image'    => self::$tree['param']['article_image'],
                    'classify_id'      => self::$tree['param']['classify_id'],
                    'sort'             => self::$tree['param']['sort'],
                    'is_recommend'     => self::$tree['param']['is_recommend'],
                    'article_status'   => self::$tree['param']['article_status']
                ]);
                if ($code > 0) {
                    $data = ['message' => '修改成功'];
                }
            }

        }
        return json_encode($data);
    }
}